/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.mine.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.impl.util.json.JSONArray;
import org.activiti.engine.impl.util.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.mine.entity.Hole;
import com.thinkgem.jeesite.modules.mine.entity.Mine;
import com.thinkgem.jeesite.modules.mine.entity.Point;
import com.thinkgem.jeesite.modules.mine.entity.Roadway;
import com.thinkgem.jeesite.modules.mine.service.HoleService;
import com.thinkgem.jeesite.modules.mine.service.MineService;
import com.thinkgem.jeesite.modules.mine.service.PointService;
import com.thinkgem.jeesite.modules.mine.service.RoadwayService;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;

/**
 * 矿井信息Controller
 * @author developer
 * @version 2018-10-27
 */
@Controller
@RequestMapping(value = "${adminPath}/mine/threeView")
public class ThreeViewController extends BaseController {

	@Autowired
	private MineService mineService;
	@Autowired
	private RoadwayService roadwayService;
	@Autowired
	private HoleService holeService;
	@Autowired
	private PointService pointService;
	
	@RequiresPermissions("mine:threeView:view")
	@RequestMapping(value = {"getInfo", ""})
	@ResponseBody
	public  String getInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String type = request.getParameter("type");
		String id = request.getParameter("id");
		ObjectMapper objectMapper = new ObjectMapper();
		JSONObject job = new JSONObject();
		if(type.equals("MJ")) {
			job.accumulate("type", "MJ");
			job.accumulate("object",  objectMapper.writeValueAsString(mineService.get(id)));
		}else if(type.equals("XD")) {
			job.accumulate("type", "XD");
			job.accumulate("object",objectMapper.writeValueAsString(roadwayService.get(id)));
		}else if(type.equals("KD")) {
			job.accumulate("type", "KD");
			job.accumulate("object", objectMapper.writeValueAsString(holeService.get(id)));
		}else if(type.equals("TC")) {
			job.accumulate("type", "TC");
			job.accumulate("object", objectMapper.writeValueAsString(pointService.get(id)));
		}
		return job.toString();
	}
	
	@RequiresPermissions("mine:threeView:view")
	@RequestMapping(value = {"getRoadwayInfoById"})
	public  String getRoadwayInfoById(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
		String id = request.getParameter("id");
		Roadway roadway = roadwayService.get(id);
		model.addAttribute("roadway",roadway);
		List<Hole> holes = holeService.findListByRoadwayId(id);
		model.addAttribute("holes",holes);
		return "modules/mine/threeViewMain";
	}
	
	@RequiresPermissions("mine:threeView:view")
	@RequestMapping(value = {"godPerspective"})
	public  String godPerspective(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception {
		String id = request.getParameter("id");
		Roadway roadway = roadwayService.get(id);
		model.addAttribute("roadway",roadway);
		List<Hole> holes = holeService.findListByRoadwayId(id);
		model.addAttribute("holes",holes);
		return "modules/mine/godPerspective";
	}
	
	@RequiresPermissions("mine:threeView:view")
	@RequestMapping(value = {"3dRation"})
	public String ration(HttpServletRequest request,Model model) {
		List <Point>list = pointService.findListByHoleId(request.getParameter("holeId"));
		model.addAttribute("length",holeService.get(request.getParameter("holeId")).getLength());
		JSONArray ja = new JSONArray();
		for(int i=0;i<list.size();i++) {
			Point point = list.get(i);
			JSONObject job = new JSONObject();
			job.accumulate("type",point.getComposition());
			Dict dict = DictUtils.getDict(point.getComposition(),"probe_type", "");
			job.accumulate("name",dict.getLabel());
			job.accumulate("color",dict.getRemarks());
			if(i ==0) {
				job.accumulate("data",new double[] {0,point.getPosition()});
			}else {
				job.accumulate("data",new double[] {list.get(i-1).getPosition(),point.getPosition()});
			}
			ja.put(job);
		}
		model.addAttribute("list",ja.toString());
		return "modules/mine/ratio";
	}
	
	
	
}